<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>后台管理模板</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="../../layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="../../css/global.css" media="all">
	<link rel="stylesheet" type="text/css" href="http://www.jq22.com/jquery/font-awesome.4.6.0.css">
	<link rel="stylesheet" href="../../datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.css">
</head>
<body>
	<div class="layui-layout layui-layout-admin" style="border-bottom: solid 5px #1aa094;">
		<!--头部导航-->
		<div class="layui-header header header-demo" ></div>
		<!--左侧导航-->
		<div class="layui-side layui-bg-black" id="admin-side">
		</div>
		<!--右侧具体内容-->
		<div class="layui-body" style="bottom: 0;border-left: solid 2px #1AA094;padding:20px" id="admin-body">
			<div class="title">修改管理员信息</div>
			<hr/>
			<form class="layui-form" action="manager.html" >
				<div class="layui-form-item">
					<img id="edit_adminUrl" src="../../images/demo1.png" style="margin:20px 20px 20px 50px;width: 60px"><div class="layui-btn layui-btn-radius layui-btn-primary" style="margin:20px 10px">修改头像</div>
				</div>
				<div class="layui-form-item" style="margin-right: 800px">
					<label class="layui-form-label">用户名</label>
					<div class="layui-input-block">
						<input type="text" name="edit_adminName" required  lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input" value="管理员1号">
					</div>
				</div>
				<div class="layui-form-item" style="margin-right: 800px">
					<label class="layui-form-label">昵称</label>
					<div class="layui-input-block">
						<input type="text" name="edit_adminNickname" required  lay-verify="required" placeholder="请输入昵称" autocomplete="off" class="layui-input" value="1200121">
					</div>
				</div>

				<div class="layui-form-item">
					<label class="layui-form-label">性别</label>
					<div class="layui-input-block">
						<input type="radio" name="edit_adminSex" value="男" title="男" id="sex_male">
						<input type="radio" name="edit_adminSex" value="女" title="女" id="sex_female">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">类型</label>
					<div class="layui-input-block">
						<input type="radio" name="edit_adminPower" value="1" title="超级管理员" >
						<input type="radio" name="edit_adminPower" value="0" title="普通管理员">
					</div>
				</div>
			</form>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="formDemo" id="confirmedit">确认修改</button>
				</div>
			</div>
		</div>
		<!--底部导航-->
		<div class="layui-footer footer footer-demo" id="admin-footer"></div>
		<!--JS模块-->
		<script type="text/javascript" src="../../layui/layui.js"></script>
		<script src="../../js/jquery-1.12.3.min.js"></script>
		<script src="../../js/index.js"></script>
		<script>
			layui.use('form', function() {
				var form = layui.form();
						//alert(window.location.href)
						C1 = window.location.href.split("?")[1];
						managerid = C1.split("=")[1];
						//alert(C1);
						//alert(managerid);

						var data = {
							"code": 200,
							"admin": {
								"adminId": 0412,
								"adminSex": "女",
								"adminName": "hwjlmn",
								"adminPower": 0,
								"adminUrl": "../../images/find_1.png",
								"adminNickname": "hwjlmn"
							}
						}

						$.ajax({
							url: "/randomdelivery/AdminController/Admin/" + managerid,
							type: "get",
							async: true,
							dataType: "json",
							contentType: "application/json",
							success: function(data) {
								personinfo(data);
							},
							error: function() {
								alert("服务器出错");
							}
						})
						//personinfo(data)

						function personinfo(data) {
							$("#edit_adminUrl").attr({
								"src": data.admin.adminUrl,
							});
							//$("input[name='edit_adminId']").val(data.admin.adminId)
							$("input[name='edit_adminNickname']").val(data.admin.adminNickname)
							$("input[name='edit_adminName']").val(data.admin.adminName)
							
							// $("input[name='edit_adminSex']").val(data.admin.adminSex)	
							var sex =data.admin.adminSex;
							var power = data.admin.adminPower;
							//alert(sex)
							$("input[type=radio][name='edit_adminSex'][value="+sex+"]").attr("checked",true)
							$("input[type=radio][name='edit_adminPower'][value="+power+"]").attr("checked",true)

							/*$("input[type=radio][name='edit_adminSex'][value="+sex+"]").attr("checked",'checked')
							$("input[type=radio][name='edit_adminPower'][value="+power+"]").attr("checked",'checked')*/
							// alert(data.admin.adminPower)
							/*	
							if(sex=="男")
							{
								$("#sex_male").attr("checked","checked");
								//$("input[name='edit_adminSex']:eq[0]").attr("checked","checked");
							}
							else if (sex=="女") 
							{
								$("input[name='edit_adminSex']eq[1]").attr("checked","checked");
							}*/
							form.render();

						}
						// $(document).on("click","#confirmedit", function() {
						$("#confirmedit").on("click", function() {
							//alert(managerid)
							//alert($("#edit_adminUrl").attr("src"))
							/*alert($("input[name='edit_adminSex']:checked").val())
							alert($("input[name='edit_adminPower']:checked").val())*/
							var adminInfomation = {
									"adminId":managerid,
									"adminName": $("input[name='edit_adminName']").val(),
									"adminNickname": $("input[name='edit_adminNickname']").val(),
									"adminSex": $("input[name='edit_adminSex']:checked").val(),
									"adminPower": $("input[name='edit_adminPower']:checked").val(),
									"adminUrl": $("#edit_adminUrl").attr("src"),
									}
							$.ajax({
								url: "/randomdelivery/AdminController/Admin",
								type: "PUT",
								data: JSON.stringify(adminInfomation),
								async: true,
								dataType: "json",
								contentType: "application/json",
								success: function(data) {	
									ifeditDone(data)
								},
								error: function() {
									alert("服务器出错");
								}
							})
							//alert()

							function ifeditDone(data) {
								if (data.code == 200) {

									//window.location.href = "manager.html"
									alert("编辑成功")
									window.location.reload();
								} else {
									if(data.result=="用户名已存在")
										alert("修改失败,用户名已存在")
									else{
										alert("对不起,服务器发生未知错误")
										window.location.reload();	
									}
								}
							}

						});
					})
				</script>
			</div>
		</body>
		</html>